<?php

/**
 * 摇一摇
 */
class shake_insertlog
{
    private $_num;
    
    public function insert(){
        set_time_limit(0);
        
        $num = 10;
        $data = redis2("zrange","allLogData66",array(0,$num));
        if($data){
            foreach($data as $thisArr){
                $thisArrNow = C::t("#shake#shake")->dataTurn($thisArr,"get");
                echo json_encode($thisArrNow)."\n";
                $thisInsertBool = C::t("#shake#shake")->insertLog($thisArrNow);
                if($thisInsertBool){
                    echo $thisInsertBool."\n";
                    redis2("zrem","allLogData66",$thisArr);
                }
                $lastTime = time();
                redis2("rm","shake_sh");
                redis2("set","shake_sh",$lastTime);
            }
            if(count($data)>=$num){
                $this->insert();
            }
        }
    }
    
    public function repeatSend(){ //重新发送
        $this->_num = intval($this->_num) + 1;
        $start = ($this->_num-1)*10;
        
        DB::SetMainDB(DB_ESHOP);
        $allData = DB::fetch_all("select * from %t where send_status=0 limit %d,10",array("es_shake_log",$start));
        if($allData){
            foreach($allData as $thisK => $thisV){
                $thisData = array();
                $thisData = $thisV;
                if($thisV['prize_json']){
                    $thisJson = json_decode($thisV['prize_json'],true);
                    foreach($thisJson as $key => $value){
                        if($key=='coupon_stock_id' && $value=='870077'){
                            $thisData[$key] = '869690';
                        }else{
                            $thisData[$key] = $value;
                        }
                        
                    }
                }
                //$thisData['coupon_stock_id']=='869690';
                //echo json_encode($thisData)."\n";
                $thisTime = microtime(true);
                $thisString = C::t("#shake#shake")->dataTurn($thisData);
                echo $thisString."\n";
                $thisLogData = array($thisTime,$thisString);
                
                
                //echo $thisLogData."\n";
                redis2("zadd","allLogData66",$thisLogData);
            }
            if(count($allData)>=10){
                $this->repeatSend();
            }
        }
    }
}